import { createRouter, createWebHistory } from 'vue-router'
import loading from '../utils/loading'

// 页面组件
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    meta: {
      title: '关于'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫 - 显示loading
router.beforeEach((to, from, next) => {
  // 显示loading
  loading.show();
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - ${import.meta.env.VITE_APP_TITLE}`;
  }
  
  next();
});

// 全局后置钩子 - 隐藏loading
router.afterEach(() => {
  // 模拟网络延迟，实际使用时可以根据需要调整或移除
  setTimeout(() => {
    loading.hide();
  }, 300);
});

export default router